/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.container.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.toools.gwt.widget.client.bc.BasicControl;
import org.toools.gwt.widget.client.bc.BasicListRenderer;
import org.toools.gwt.widget.client.bc.BasicTogglePanel;
import org.toools.gwt.widget.client.bc.DefaultCallback;
import org.toools.gwt.widget.client.bc.DefaultDeleteTogglePanel;
import org.toools.gwt.widget.client.bc.RelationControl;
import org.toools.gwt.widget.client.cloud.CloudListener;
import org.toools.gwt.widget.client.cloud.TagVO;
import org.toools.gwt.wmc.client.additive.gui.AdditivePanel;
import org.toools.gwt.wmc.client.companypart.CompanyPartService;
import org.toools.gwt.wmc.client.companypart.gui.CompanyPartControl;
import org.toools.gwt.wmc.client.companypart.gui.CompanyPartPanel;
import org.toools.gwt.wmc.client.container.ContainerService;
import org.toools.gwt.wmc.client.container.ContainerServiceAsync;
import org.toools.gwt.wmc.client.disposer.DisposerService;
import org.toools.gwt.wmc.client.disposer.gui.DisposerControl;
import org.toools.gwt.wmc.client.disposer.gui.DisposerPanel;
import org.toools.gwt.wmc.client.waste.WasteService;
import org.toools.gwt.wmc.client.waste.gui.WasteControl;
import org.toools.gwt.wmc.client.waste.gui.WastePanel;
import org.toools.gwt.wmc.jpa.Additive;
import org.toools.gwt.wmc.jpa.CompanyPart;
import org.toools.gwt.wmc.jpa.Container;
import org.toools.gwt.wmc.jpa.Disposer;
import org.toools.gwt.wmc.jpa.Waste;

/**
 *
 * @author Manuel
 */
public class ContainerListRenderer extends BasicListRenderer{

    private Container cont = null;

    private CompanyPartControl cpc = null;
    private WasteControl wc = null;
//    private DisposerControl dc = null;

    BasicTogglePanel companyparts = new BasicTogglePanel("set companyparts", 1, true) {
        public void onActive() {
            clear();
            addChild(new CompanyPartsRelation((Container)getTag().getAttachedBusinessObject()));
        }
    };

    BasicTogglePanel waste = new BasicTogglePanel("set waste", 1, true) {
        public void onActive() {
            clear();
            addChild(new WasteRelation((Container)getTag().getAttachedBusinessObject()));
        }
    };

    BasicTogglePanel disposer = new BasicTogglePanel("set disposer", 1, true) {
        public void onActive() {
            clear();
            addChild(new DisposerRelation((Container)getTag().getAttachedBusinessObject()));
        }
    };

    org.toools.gwt.widget.client.bc.ToggleButton cptb = new org.toools.gwt.widget.client.bc.ToggleButton("company parts", false) {
        public void toggle() {
            super.toggle();
            if(cpc != null) {
                cpc.setVisible(isOn());
            }
        }
    };

    org.toools.gwt.widget.client.bc.ToggleButton wastetb = new org.toools.gwt.widget.client.bc.ToggleButton("waste", false) {
        public void toggle() {
            super.toggle();
            if(wc != null) {
                wc.setVisible(isOn());
            }
        }
    };

//    org.toools.gwt.widget.client.bc.ToggleButton disposertb = new org.toools.gwt.widget.client.bc.ToggleButton("disposer", false) {
//        public void toggle() {
//            super.toggle();
//            if(dc != null) {
//                dc.setVisible(isOn());
//            }
//        }
//    };

    public ContainerListRenderer(TagVO tag, BasicControl bc) {
		super(tag, bc);

        cont = (Container)tag.getAttachedBusinessObject();

        cpc = new CompanyPartControl(CompanyPartService.Util.getInstance(), cont);
        wc = new WasteControl(WasteService.Util.getInstance(), cont);
        //dc = new DisposerControl(DisposerService.Util.getInstance());

        cpc.setVisible(false);
        wc.setVisible(false);
        //dc.setVisible(false);

//        questionable if this should be done here
//        addLeft(cptb);
        addLeft(wastetb);
        //addLeft(disposertb);

        addControl(cpc);
        addControl(wc);
        //addControl(dc);

//        questionable if this should be done here
//        addButton(companyparts);
        addButton(waste);
        addButton(disposer);
	}

    @Override
    public void setTag(TagVO tag) {
		Container ac = (Container) tag.getAttachedBusinessObject();
		setTitle(ac.getName());
        if(ac.getCompanyParts() != null) {
            if(ac.getCompanyParts().size() == 0) {
                this.setSubTitle("no companyparts");
            } else if(ac.getCompanyParts().size() == 1) {
                this.setSubTitle("one companypart");
            } else if(ac.getCompanyParts().size() > 1) {
                this.setSubTitle("" + ac.getCompanyParts().size() + " companyparts");
            }
        }
        if(ac.getWaste() != null) {
            if(ac.getWaste().size() == 0) {
                this.setSubTitle(this.getSubTitle() + " - no waste");
            } else if(ac.getWaste().size() == 1) {
                this.setSubTitle(this.getSubTitle() + " - one waste");
            } else if(ac.getWaste().size() > 1) {
                this.setSubTitle(this.getSubTitle() + " - " + ac.getWaste().size() + " waste");
            }
        }
        if(ac.getDisposer() != null) {
            this.setSubTitle(this.getSubTitle() + " - has disposer");
        }
	}

    public Widget getChildBasicPanel() {
        Object o = getBasicControl().getBasicPanel();
        if (o instanceof WastePanel) {
            WastePanel wp = (WastePanel) o;
            wp.addParent((Container)getTag().getAttachedBusinessObject());
            return wp;
        } else if (o instanceof DisposerPanel) {
            DisposerPanel dp = (DisposerPanel) o;
            dp.addChild((Container)getTag().getAttachedBusinessObject());
            return dp;
        } else if (o instanceof CompanyPartPanel) {
            CompanyPartPanel dp = (CompanyPartPanel) o;
            dp.addChildContainer((Container)getTag().getAttachedBusinessObject());
            return dp;
        } else
            return null;
    }
    public String getExpandMessage() {
        String ret = "<div><img src='folder.png'/> " + cont.getWaste().size() + " Waste childs</div>";
        ret += "<div><img src='folder.png'/> " + cont.getCompanyParts().size() + " Company Part childs</div>";
        return ret;
    }
    public boolean getExpandVisible() {
        return ((cont.getWaste().size() > 0) || (cont.getCompanyParts().size() > 0) || (cont.getDisposer() != null));
    }

    public DefaultDeleteTogglePanel getDefaultDeleteTogglePanel() {
        return new DefaultDeleteTogglePanel(getBasicControl(), getTag().getAttachedBusinessObject()) {
            @Override
            public boolean valid() {
                Container cont = (Container)getTag().getAttachedBusinessObject();

                if ((cont.getWaste().size() > 0) || (cont.getCompanyParts().size() > 0) || (cont.getDisposer() != null)) {
                    return false;
                } else {
                    return true;
                }
            }
            public String getDeleteConfirmMessage() {
                if(!valid())
                    return "deletion not possible with childs.";
                else
                    return getBasicControl().getDeleteConfirmMessage();
            }
        };
    }

    public class DisposerRelation extends RelationControl {
        Container container;
        public DisposerRelation(Container cont) {
            super(false, true, true, false);
            this.container = cont;
            DisposerService.Util.getInstance().getDisposers(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }
        public TagVO getTagVO(Object bp) {
            Disposer disposer = (Disposer) bp;
            TagVO result = new TagVO("" + disposer.getId(), disposer.getName(), 0, 0);
            result.setAttachedBusinessObject(disposer);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<Disposer>)result).toArray()));
                if(cont.getDisposer() != null) {
                    getRelationList().setActive(""+cont.getDisposer().getId());
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                TagVO tag = (TagVO) tags.next();
                container.setDisposer((Disposer)tag.getAttachedBusinessObject());
                ContainerService.Util.getInstance().update(container, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }

    public class WasteRelation extends RelationControl {
        Container container;
        public WasteRelation(Container cont) {
            super(false, true, true, false);
            this.container = cont;
            WasteService.Util.getInstance().getWastes(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }
        public TagVO getTagVO(Object bp) {
            Waste waste = (Waste) bp;
            TagVO result = new TagVO("" + waste.getGtin(), waste.getWasteDesc(), 0, 0);
            result.setAttachedBusinessObject(waste);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<Waste>)result).toArray()));
                if((container.getWaste() != null) && (container.getWaste().size() > 0)) {
                    String[] active = new String[container.getWaste().size()];
                    int j = 0;
                    for(Iterator i = container.getWaste().iterator(); i.hasNext();) {
                        Waste waste = (Waste) i.next();
                        active[j++] = "" + waste.getGtin();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                Collection<Waste> wastes = new Vector<Waste>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    wastes.add((Waste)tag.getAttachedBusinessObject());
                }
                container.setWaste(wastes);
                ((ContainerServiceAsync)getBasicControl().getService()).update(container, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }

    private class CompanyPartsRelation extends RelationControl {
        Container container;
        public CompanyPartsRelation(Container cont) {
            super(false, true, true, false);
            this.container = cont;
            CompanyPartService.Util.getInstance().getCompanyParts(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }

        public TagVO getTagVO(Object bp) {
            CompanyPart cp = (CompanyPart) bp;
            TagVO result = new TagVO("" + cp.getId(), cp.getName(), 0, 0);
            result.setAttachedBusinessObject(cp);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<CompanyPart>)result).toArray()));
                if((cont.getCompanyParts() != null) && (cont.getCompanyParts().size() > 0)) {
                    String[] active = new String[cont.getCompanyParts().size()];
                    int j = 0;
                    for(Iterator i = cont.getCompanyParts().iterator(); i.hasNext();) {
                        CompanyPart cp = (CompanyPart) i.next();
                        active[j++] = "" + cp.getId();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                List<CompanyPart> cps = new ArrayList<CompanyPart>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    cps.add((CompanyPart)tag.getAttachedBusinessObject());
                }
                container.setCompanyParts(cps);
                ContainerService.Util.getInstance().update(container, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }

}
